<?php

// ini_set('display_errors',1);
// error_reporting(E_ALL);

/**
 * REGISTER API NOSQL
 * @author HxBreak
 * @Description: todo
 * @Date 2017年6月7日 上午11:40:45 
 * @File register.php
 */

require '../vendor/autoload.php';
require 'UserManager.php';

use Base64Url\Base64Url;

session_set_cookie_params(1200);
session_start();

$action = $_REQUEST['action'];
$typedname = $_REQUEST['username'];
$typedpassword = $_REQUEST['password'];
$typedmail = $_REQUEST['email'];
$sex = $_REQUEST['sex'];
$brithday = $_REQUEST['brithday'];
$nickname = $_REQUEST['nickname'];
$ticket = $_REQUEST['ticket'];
$ccode = $_REQUEST['ccode'];
$time = time();
$msg = array();

$userManager = new UserManager();

$typedpassword = Base64Url::decode($typedpassword);
//verify username, 
if($action == 'checkusername'){
    $msg['errorcode'] = (boolean)($userManager->checkString($typedname) 
    && $userManager->checkStringLength($typedname)
    && !$userManager->lookupUser($typedname));
}else if($action == 'checkpassword'){
    $msg['errorcode'] = (boolean)($userManager->checkStringLength($typedpassword))
    && $userManager->matchPassword($typedpassword);
}else if($action == 'checkemail'){
    $msg['errorcode'] = (boolean)(!$userManager->lookupEmail($typedmail));
}else if($action == 'verify'){
    if($_SESSION['authed']){
        $msg['errorcode'] = (boolean)($userManager->insertVerifyUser($typedname, $typedpassword, $typedmail));
        $_SESSION['authed'] = false;
    }else{
        $msg['errorcode'] = false;
    }
}else if($action == 'checkcode'){
    $msg['errorcode'] = (boolean)($userManager->insertUser($ticket));
}else if($action == 'test'){
    $msg['errorcode'] = (boolean)$userManager->matchPassword($typedpassword);
}else if($action == 'doauth'){
    if($_SESSION['authed']){
        $msg['errorcode'] = true;
    }else if(strcasecmp($_SESSION['phrase'], $ccode) == 0){
        $_SESSION['authed'] = true;
        $msg['errorcode'] = true;
    }else{
        $msg['errorcode'] = false;
    }
}else{
    $msg['errorcode'] = false;
}

echo json_encode($msg);
?>